{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e622029a-4615-4780-b001-8345be212018",
   "metadata": {},
   "source": [
    "# 任务\n",
    "* 以自写的Cart决策树为基学习器，利用AdaBoost进行集成学习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "135f9a0d-4f4a-4b99-b4b5-16b753b641f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import sys\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "f154f19b-39f0-4142-a23a-2486e3a14159",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将项目根目录添加到 Python 的模块搜索路径\n",
    "sys.path.append(os.path.abspath('../../'))  # 替换为项目根目录\n",
    "\n",
    "# 现在可以正常导入模块\n",
    "from ModelDefination.Ada.Ada import AdaBoost\n",
    "from ModelDefination.Ada.Ada import CARTDecisionTree"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f765b848-57d0-44ab-b28e-b772a3a769d7",
   "metadata": {},
   "source": [
    "### 读取特征选择后的数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "e7e4d533-84be-4b63-a0c7-62487fcac3b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "from joblib import dump, load\n",
    "\n",
    "final_train_data = pd.read_csv('../../FeatureSelectedData/selected_train.csv')\n",
    "final_X_train = final_train_data.iloc[:, :-1]\n",
    "final_y_train = final_train_data.iloc[:, -1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "8c2cb8b3-e878-4b89-b1b0-c17f3f5e14bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 再试一下 AdaBoost\n",
    "def stump():\n",
    "    return CARTDecisionTree(max_depth=5)  # 树桩\n",
    "\n",
    "ada_clf = AdaBoost(base_estimator=stump, n_estimators=10, learning_rate=1.0)\n",
    "ada_clf.fit(final_X_train, final_y_train)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e127d35c-f4f2-4ffc-abb8-9f219cc3b816",
   "metadata": {},
   "source": [
    "### 保存模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "2f89cf95-2201-45fd-b06c-cfe9cb18fd23",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model saved as ada_model.joblib\n"
     ]
    }
   ],
   "source": [
    "dump(ada_clf, '../../ModelFile/Ada/ada_model.joblib')\n",
    "print(\"Model saved as ada_model.joblib\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dbd12701-336d-4332-bf40-0dd080c5039f",
   "metadata": {},
   "source": [
    "### 将 `Ada` 决策树封装为 `Ada()` 函数进行预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "e82a2152-375e-4441-a90a-5cf59bbaa387",
   "metadata": {},
   "outputs": [],
   "source": [
    "def Ada(X):\n",
    "    clf = load('../../ModelFile/Ada/ada_model.joblib')\n",
    "    y_predicted = clf.predict(X)\n",
    "    return y_predicted"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2037e870-9869-433b-bf85-b92719c1afac",
   "metadata": {},
   "source": [
    "### 调用 `Ada` 函数预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "0c349cc5-8480-4599-a9d8-af66c57d4ec6",
   "metadata": {},
   "outputs": [],
   "source": [
    "final_test_data = pd.read_csv('../../FeatureSelectedData/selected_test.csv')\n",
    "result_file = pd.read_csv('../../RawData/sample_submission.csv')\n",
    "predictions = Ada(final_test_data)\n",
    "predictions_bool = (predictions == 1)\n",
    "result_file[\"Transported\"] = pd.Series(predictions_bool, result_file.index).astype(bool)\n",
    "result_file.to_csv('../../predictions/Ada/result_ada.csv', index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "631e804b-f0b3-41ee-a16c-0f93b6839a51",
   "metadata": {},
   "source": [
    "### 将 `ada_svm.csv` 放在 `kaggle` 上评分"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
